{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# sicherman_dice"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/sicherman_dice.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/sicherman_dice.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "  Sicherman Dice in Google CP Solver.\n",
    "\n",
    "  From http://en.wikipedia.org/wiki/Sicherman_dice\n",
    "  \"\"\n",
    "  Sicherman dice are the only pair of 6-sided dice which are not normal dice,\n",
    "  bear only positive integers, and have the same probability distribution for\n",
    "  the sum as normal dice.\n",
    "\n",
    "  The faces on the dice are numbered 1, 2, 2, 3, 3, 4 and 1, 3, 4, 5, 6, 8.\n",
    "  \"\"\n",
    "\n",
    "  I read about this problem in a book/column by Martin Gardner long\n",
    "  time ago, and got inspired to model it now by the WolframBlog post\n",
    "  \"Sicherman Dice\": http://blog.wolfram.com/2010/07/13/sicherman-dice/\n",
    "\n",
    "  This model gets the two different ways, first the standard way and\n",
    "  then the Sicherman dice:\n",
    "\n",
    "  x1 = [1, 2, 3, 4, 5, 6]\n",
    "  x2 = [1, 2, 3, 4, 5, 6]\n",
    "  ----------\n",
    "  x1 = [1, 2, 2, 3, 3, 4]\n",
    "  x2 = [1, 3, 4, 5, 6, 8]\n",
    "\n",
    "\n",
    "  Extra: If we also allow 0 (zero) as a valid value then the\n",
    "  following two solutions are also valid:\n",
    "\n",
    "  x1 = [0, 1, 1, 2, 2, 3]\n",
    "  x2 = [2, 4, 5, 6, 7, 9]\n",
    "  ----------\n",
    "  x1 = [0, 1, 2, 3, 4, 5]\n",
    "  x2 = [2, 3, 4, 5, 6, 7]\n",
    "\n",
    "  These two extra cases are mentioned here:\n",
    "  http://mathworld.wolfram.com/SichermanDice.html\n",
    "\n",
    "  Compare with these models:\n",
    "  * MiniZinc: http://hakank.org/minizinc/sicherman_dice.mzn\n",
    "  * Gecode: http://hakank.org/gecode/sicherman_dice.cpp\n",
    "\n",
    "  This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
    "  Also see my other Google CP Solver models:\n",
    "  http://www.hakank.org/google_or_tools/\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "from ortools.constraint_solver import pywrapcp\n",
    "\n",
    "\n",
    "def main():\n",
    "\n",
    "  # Create the solver.\n",
    "  solver = pywrapcp.Solver(\"Sicherman dice\")\n",
    "\n",
    "  #\n",
    "  # data\n",
    "  #\n",
    "  n = 6\n",
    "  m = 10\n",
    "\n",
    "  # standard distribution\n",
    "  standard_dist = [1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1]\n",
    "\n",
    "  #\n",
    "  # declare variables\n",
    "  #\n",
    "\n",
    "  # the two dice\n",
    "  x1 = [solver.IntVar(0, m, \"x1(%i)\" % i) for i in range(n)]\n",
    "  x2 = [solver.IntVar(0, m, \"x2(%i)\" % i) for i in range(n)]\n",
    "\n",
    "  #\n",
    "  # constraints\n",
    "  #\n",
    "  # [solver.Add(standard_dist[k] == solver.Sum([x1[i] + x2[j] == k+2 for i in range(n) for j in range(n)]))\n",
    "  # for k in range(len(standard_dist))]\n",
    "  for k in range(len(standard_dist)):\n",
    "    tmp = [solver.BoolVar() for i in range(n) for j in range(n)]\n",
    "    for i in range(n):\n",
    "      for j in range(n):\n",
    "        solver.Add(tmp[i * n + j] == solver.IsEqualCstVar(x1[i] + x2[j], k + 2))\n",
    "    solver.Add(standard_dist[k] == solver.Sum(tmp))\n",
    "\n",
    "  # symmetry breaking\n",
    "  [solver.Add(x1[i] <= x1[i + 1]) for i in range(n - 1)],\n",
    "  [solver.Add(x2[i] <= x2[i + 1]) for i in range(n - 1)],\n",
    "  [solver.Add(x1[i] <= x2[i]) for i in range(n - 1)],\n",
    "\n",
    "  #\n",
    "  # solution and search\n",
    "  #\n",
    "  solution = solver.Assignment()\n",
    "  solution.Add(x1)\n",
    "  solution.Add(x2)\n",
    "\n",
    "  # db: DecisionBuilder\n",
    "  db = solver.Phase(x1 + x2, solver.INT_VAR_SIMPLE, solver.ASSIGN_MIN_VALUE)\n",
    "\n",
    "  solver.NewSearch(db)\n",
    "  num_solutions = 0\n",
    "  while solver.NextSolution():\n",
    "    print(\"x1:\", [x1[i].Value() for i in range(n)])\n",
    "    print(\"x2:\", [x2[i].Value() for i in range(n)])\n",
    "    print()\n",
    "\n",
    "    num_solutions += 1\n",
    "  solver.EndSearch()\n",
    "\n",
    "  print()\n",
    "  print(\"num_solutions:\", num_solutions, \"solver.solutions:\",\n",
    "        solver.Solutions())\n",
    "  print(\"failures:\", solver.Failures())\n",
    "  print(\"branches:\", solver.Branches())\n",
    "  print(\"WallTime:\", solver.WallTime())\n",
    "  print(\"MemoryUsage:\", solver.MemoryUsage())\n",
    "  print(\"SearchDepth:\", solver.SearchDepth())\n",
    "  print(\"SolveDepth:\", solver.SolveDepth())\n",
    "  print(\"stamp:\", solver.Stamp())\n",
    "  print(\"solver\", solver)\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
